
import 'package:cloudapp/esdk/esync.dart';
import 'package:cloudapp/esdk/response/file_list_item.dart';
import 'package:cloudapp/pages/base.dart';
import 'package:flutter/material.dart';
import 'package:photo_view/photo_view_gallery.dart';
import 'dart:convert' as convert;

class AlbumViewerPage extends StatefulWidget {
  static const pageName = "/viewer/album";
  final AlbumViewerArguments arguments;
  AlbumViewerPage({Key key, @required this.arguments}):super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _AlbumViewerPage();
  }
}

class _AlbumViewerPage extends BaseState<AlbumViewerPage> {
  PageController pageController;

  @override
  void initState() {
    super.initState();
    pageController = PageController(initialPage: widget.arguments.index);
  }

  @override
  Widget build(BuildContext context) {
    var args = widget.arguments;
    var albums = args.albums;
    return Center(
      child: PhotoViewGallery.builder(
        enableRotation: true,
        itemCount: albums.length,
        pageController: pageController,
        builder: (context, index) {
          var item = albums[index];
          var imgUrl = ESync.i().getFileRawUrl(item.id);
          var thumb = convert.base64Decode(item.thumb);

          return PhotoViewGalleryPageOptions.customChild(
            child: FadeInImage.memoryNetwork(
              placeholder: thumb,
              image: imgUrl,
              placeholderScale: 0.1,
              fadeInDuration: const Duration(milliseconds: 1),
              fadeOutDuration: const Duration(milliseconds: 1),
            ),
          );
        },
      ),
    );
  }
}
